<?php
namespace app\restaurant\controller;
use app\AdminRest;

use app\ApiRest;
use app\diy\model\DiyModel;
use app\restaurant\model\Cate;
use app\restaurant\model\StoreStaff;
use think\App;

use app\restaurant\model\Goods as model;

use app\restaurant\model\Coupon;

use app\restaurant\model\CouponRecord;

use think\facade\Db;


class IndexCoupon extends ApiRest
{

    protected $model;

    protected $coupon_model;

    protected $coupon_record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->coupon_model = new Coupon();

        $this->coupon_record_model = new CouponRecord();
        //处理过期优惠券
        $this->coupon_record_model->endRecord($this->getUserId());

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 17:32
     * @功能说明:优惠券列表
     */
    public function couponList(){

        $data = $this->coupon_model->couponCanGetPage($this->_uniacid,$this->getUserId());

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 17:34
     * @功能说明:领取优惠券
     */
    public function getCoupon(){

        $input    = $this->_input;

        $coupon_id = $input['id'];

        Db::startTrans();

        foreach ($coupon_id as $value){

            $coupon = $this->coupon_model->couponInfo(['id'=>$value,'status'=>1]);

            if(!empty($coupon)){

                $where = [];

                $where[] = ['coupon_id','=',$coupon['id']];

                $where[] = ['user_id','=',$this->getUserId()];

                $num = $this->coupon_record_model->where($where)->count();

                $num = is_numeric($num)?$num:0;

                if($coupon['stock']<1){

                    $this->errorMsg('库存不足');
                }

                if($coupon['limit_num']<=$num){

                    $this->errorMsg('领取已到上限');

                }
                //领取优惠券
                $res = $this->coupon_record_model->couponGet($this->getUserId(),$this->_uniacid,$coupon);

                if(!empty($res['code'])){

                    $this->errorMsg($res['msg']);
                }

            }

        }

        Db::commit();

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-21 11:09
     * @功能说明:卡券详情
     */
    public function couponInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->coupon_model->couponInfo($dis);

        $where[] = ['coupon_id','=',$data['id']];

        $where[] = ['user_id','=',$this->getUserId()];

        $num = $this->coupon_record_model->where($where)->count();
        //可以领取到次数
        $data['can_get_num'] = $data['limit_num'] - $num>0?$data['limit_num'] - $num:0;
        //领取记录
        $data['record'] = $this->coupon_record_model->where(['coupon_id'=> $input['id']])->order('id desc')->paginate(10)->toArray();

        if ( !empty(  $data['record']['data'] ) )
        {

            foreach (  $data['record']['data'] as $index => &$item )
            {
//
                $user = longbingGetUser($item[ 'user_id' ] ,$this->_uniacid);

                if ( mb_strlen( $user[ 'nickName' ], 'utf8' ) > 4 )
                {
                    $user[ 'nickName' ] = mb_substr( $user[ 'nickName' ], 0, 4, "UTF-8" );
                }

                $item[ 'user_info' ] = $user;
            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-23 15:20
     * @功能说明:用户优惠券列表
     */
    public function userCouponList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['user_id','=',$this->getUserId()];

        if(isset($input['status'])){

            $dis[] = ['status','=',$input['status']];
        }

        $data = $this->coupon_record_model->where($dis)->order('id desc')->paginate(10)->toArray();

        $data['last_staff_id'] = $this->getUserInfo()['last_staff_id'];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-23 15:53
     * @功能说明:
     */
    public function recordInfo(){


        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->coupon_record_model->dataInfo($dis);


        return $this->success($data);


    }









}
